package com.google.code.synchronized_player;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SettingsActivity extends Activity {
	EditText editTextIp;
	EditText editTextPort;
	Button buttonIp;
	Button buttonPort;
	Button buttonSynch;
	TextView synout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		synout = (TextView) findViewById(R.id.synout);
		editTextIp = (EditText) findViewById(R.id.ip_text);
		editTextPort = (EditText) findViewById(R.id.port_text);
		buttonIp = (Button) findViewById(R.id.ip_button);
		buttonPort = (Button) findViewById(R.id.port_button);
		buttonSynch = (Button)findViewById(R.id.synch_button);

		buttonIp.setOnClickListener(buttonIpOcl);
		buttonPort.setOnClickListener(buttonPortOcl);
		buttonSynch.setOnClickListener(buttonSynchOcl);
		
		LoadPreferences();
	}

	Button.OnClickListener buttonIpOcl = new Button.OnClickListener() {
		@Override
		public void onClick(View arg0) {
			SavePreferences("Ip", editTextIp.getText().toString());
			LoadPreferences();
		}
	};

	Button.OnClickListener buttonPortOcl = new Button.OnClickListener() {
		@Override
		public void onClick(View arg0) {
			SavePreferences("Port", editTextPort.getText().toString());
			LoadPreferences();
		}

	};
	
	Button.OnClickListener buttonSynchOcl = new Button.OnClickListener() {
		@Override
		public void onClick(View arg0) {
			synout.setText("Synchonizing...");
			SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
			String strSavedIp = sharedPreferences.getString("Ip", "");
			String strSavedPort = sharedPreferences.getString("Port", "");
			new Thread(new Client(strSavedIp, Integer.parseInt(strSavedPort))).start();
		}
	};

	private void LoadPreferences() {
		SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
		String strSavedIp = sharedPreferences.getString("Ip", "");
		String strSavedPort = sharedPreferences.getString("Port", "");
		String strSynch = sharedPreferences.getString("Synch", "");
		editTextIp.setText(strSavedIp);
		editTextPort.setText(strSavedPort);
		synout.setText(strSynch);
	}

	private void SavePreferences(String key, String value) {
		SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public void savesSettings() {

	}
}
